from rest_framework import serializers
from .models import Project, Version, ProductLine

class ProductLineSerializer(serializers.ModelSerializer):
    """产品线序列化器"""
    # 添加项目数量字段
    projects_count = serializers.SerializerMethodField()
    
    class Meta:
        model = ProductLine
        fields = ['id', 'name', 'description', 'created_at', 'updated_at', 'is_active', 'projects_count']
    
    def get_projects_count(self, obj):
        """获取产品线的项目数量"""
        return obj.projects.count()

class ProjectSerializer(serializers.ModelSerializer):
    """项目序列化器"""
    # 添加用例数量字段
    cases_count = serializers.SerializerMethodField()
    # 添加版本数量字段
    versions_count = serializers.SerializerMethodField()
    # 添加产品线名称字段
    product_line_name = serializers.ReadOnlyField(source='product_line.name', allow_null=True)
    
    class Meta:
        model = Project
        fields = ['id', 'name', 'description', 'project_type', 'product_line', 'product_line_name', 'created_at', 'updated_at', 'is_active',
                  'creator', 'department', 'cases_count', 'versions_count']
    
    def get_cases_count(self, obj):
        """获取项目的用例总数"""
        from cases.models import TestCase
        # 计算该项目下所有版本的用例总数
        return TestCase.objects.filter(version__project=obj).count()
    
    def get_versions_count(self, obj):
        """获取项目的版本数量"""
        return obj.versions.count()

class VersionSerializer(serializers.ModelSerializer):
    """项目版本序列化器"""
    # 添加项目名称字段
    project_name = serializers.ReadOnlyField(source='project.name')
    # 添加项目类型字段
    project_type = serializers.ReadOnlyField(source='project.project_type')
    # 添加用例数量字段
    cases_count = serializers.SerializerMethodField()
    
    class Meta:
        model = Version
        fields = ['id', 'project', 'project_name', 'project_type', 'version_number', 'description',
                  'created_at', 'updated_at', 'release_date', 'is_current', 'cases_count']
    
    def get_cases_count(self, obj):
        """获取版本的用例数量"""
        return obj.test_cases.count()

class ProjectListSerializer(serializers.ModelSerializer):
    """项目列表序列化器（用于项目管理页面）"""
    # 项目类型显示值
    project_type_display = serializers.CharField(source='get_project_type_display')
    # 最近版本号
    latest_version = serializers.SerializerMethodField()
    # 用例总数
    cases_count = serializers.SerializerMethodField()
    # 产品线名称
    product_line_name = serializers.ReadOnlyField(source='product_line.name', allow_null=True)
    
    class Meta:
        model = Project
        fields = ['id', 'name', 'project_type', 'project_type_display', 'product_line_name', 'latest_version', 'cases_count']
    
    def get_latest_version(self, obj):
        """获取项目的最新版本号"""
        latest_version = obj.versions.order_by('-created_at').first()
        return latest_version.version_number if latest_version else None
    
    def get_cases_count(self, obj):
        """获取项目的用例总数"""
        from cases.models import TestCase
        return TestCase.objects.filter(version__project=obj).count()